/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: tc_type.c
    Module  : (geometric) itk module.

        This file manages the tctype operations..

=======================================================================================================================
Date               Name              Description of Change
14-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
#ifdef BUILD_IMAN

#include <imantype.h>

#define  GTC_strcmp                     iman_strcmp
#define  GTC_strcat                     iman_strcat
#define  GTC_strncpy                    iman_strncpy
#define  GTCTYPE_find_type              IMANTYPE_find_type

#else

#include <tccore\tctype.h>

#define  GTC_strcmp                     tc_strcmp
#define  GTC_strcat                     tc_strcat
#define  GTC_strncpy                    tc_strncpy
#define  GTCTYPE_find_type              TCTYPE_find_type

#endif

#include <ai\sample_err.h>
#include <form\formtype.h>
#include <tccore\aom.h>
#include <sa\group.h>

#include "tc_type.h"

int GTCTYPE_is_type_of( tag_t wsoTag, const char *typeName, logical *isType )
{
    int     ifail   = ITK_ok;
    tag_t   typeTag = NULLTAG;

    *isType = false;

    // Validate the revision type
    ifail = GTCTYPE_find_type( typeName, NULL, &typeTag );

    CALL( ifail );
    if (ifail == ITK_ok)
    {
        // Ignore this error.but print to Sys log
#ifdef BUILD_IMAN
        GTCTYPE_is_definite_type_of( wsoTag, typeName, isType);
#else
        DO( AOM_is_type_of( wsoTag, typeTag, isType ) );
#endif
    }

    return ifail;
}

int GTCTYPE_is_definite_type_of( tag_t wsoTag, const char *typeName, logical *isType )
{
    int     ifail   = ITK_ok;
    char  objTypeName[WSO_name_size_c + 1];
    memset( objTypeName, 0, sizeof(objTypeName) );

    *isType = false;

    DO(WSOM_ask_object_type( wsoTag, objTypeName ));
    if(typeName != NULL && typeName[0] != '\0' && GTC_strcmp(objTypeName, typeName) == 0)
    {
        *isType = true;
    }
    return ifail;
}

int GTCTYPE_is_owner_group( tag_t wsoTag, const char *groupName, logical *isGroupOwner )
{
    int   ifail = ITK_ok;
    tag_t group = NULLTAG;
    char  name[SA_name_size_c + 1];
    memset( name, 0, sizeof(name) );

    *isGroupOwner = false;

    if ( (ifail = AOM_ask_group( wsoTag, &group )) == ITK_ok
        && (ifail = SA_ask_group_name( group, name )) == ITK_ok 
        && GTC_strcmp( name, groupName ) == 0 )
    {
        *isGroupOwner = true;
    }
    return ifail;
}

int GTCTYPE_get_master_name( const char *itemNameOrItemRevName, char masterName[WSO_name_size_c + 1] )
{
    int ifail = ITK_ok;

    masterName[0] = '\0';
    GTC_strncpy( masterName, itemNameOrItemRevName, WSO_name_size_c + 1 );
    GTC_strcat( masterName, " Master");

    return ifail;
}

int GTCTYPE_get_form_class_name( const char *itemRevName, char className[WSO_name_size_c + 1] )
{
    int   ifail                           = ITK_ok;

    tag_t typeTag                         = NULLTAG;
    char  masterName[WSO_name_size_c + 1] = { '\0' };
    char  *defClass                       = NULL;
    
    ifail = GTCTYPE_get_master_name( itemRevName, masterName );
    if (ifail != ITK_ok)
        return ifail;

    ifail = GTCTYPE_find_type( masterName, "", &typeTag );

    CALL( ifail );

    ifail = FORMTYPE_ask_def_class_name( typeTag, &defClass );
    CALL( ifail );

    GTC_strncpy( className, defClass, WSO_name_size_c + 1 );

    MEM_free( defClass );

    return ifail;
}
